package com.hsc.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsc.commonutils.R;
import com.hsc.eduservice.entity.EduTeacher;
import com.hsc.eduservice.entity.vo.EduTeacherQuery;
import com.hsc.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-10-11
 */
//@Api等注解只是复制swagger界面文档表达，对功能没有增强
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin //gateway解决了可以注掉
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation("查询所有讲师")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list(null);
//        int i =10/0; //仅仅用来测试全局异常处理是否成功生效
        return R.ok().data("items",list);
    }

    /**
     * 逻辑删除讲师
     * @param id 讲师id
     * @return
     */
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(
            @ApiParam(name = "id",value = "讲师ID",required = true)
            @PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 分页查询讲师方法
     * 注意分页前提要在配置类添加分页插件
     * @param current 当前页
     * @param limit 每页记录数
     * @return
     */
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit){
//        System.out.println(current);
//        System.out.println(limit);
//        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
//        eduTeacherService.page(pageTeacher, null);
//        List<EduTeacher> list = pageTeacher.getRecords();
//        long total = pageTeacher.getTotal();
//        return R.ok().data("rows",list).data("total",total);
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);

//        int i = 10/0;

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        eduTeacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 分页条件查询语句
     * @param current 当前页
     * @param limit 当前页记录数
     * @param eduTeacherQuery 传过来的条件，可以为空，required=false
     * @return
     */
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current ,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) EduTeacherQuery eduTeacherQuery){
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        String name = eduTeacherQuery.getName();
        Integer level = eduTeacherQuery.getLevel();
        String begin = eduTeacherQuery.getBegin();
        String end = eduTeacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }

        eduTeacherService.page(pageTeacher,wrapper);//查询语句

        long total = pageTeacher.getTotal();//总记录数
        List<EduTeacher> records = pageTeacher.getRecords(); //数据list集合
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 新增讲师
     * @param eduTeacher 讲师信息
     * @return
     */
    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 根据id查询老师
     * @param id 老师id
     * @return
     */
    @ApiOperation(value = "根据id查询老师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        if(teacher!=null){
            return R.ok().data("teacher",teacher);
        }else {
            return R.error();
        }
    }

    /**
     * 修改教师信息
     * @param eduTeacher 传过来的需要修改的信息，id必须要
     * @return
     */
    @ApiOperation(value = "根据id修改老师信息")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        if (eduTeacher.getId()==null){
            return R.error().message("信息不全");
        }
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag){
            return R.ok().message("修改教师信息成功");
        }else {
            return R.error().message("修改老师信息失败");
        }
    }

    //另一种根据id修改老师信息的写法
    /*@ApiOperation(value = "根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody Teacher teacher){
        teacher.setId(id);
        teacherService.updateById(teacher);
        return R.ok();
    }*/



}

